<template>
<block>
<!--
  - Copyright (C) 2018-2019
  - All rights reserved, Designed By www.joolun.com
  - 注意：
  - 本软件为www.joolun.com开发研制，项目使用请保留此说明
-->
<form @submit="userAddressSave">
  <view class="cu-form-group">
		<view class="title">姓名</view>
		<input placeholder="请输入姓名" name="userName" :value="userAddress.userName"/>
	</view>
  <view class="cu-form-group">
		<view class="title">联系电话</view>
		<input placeholder="请输入电话" name="telNum" :value="userAddress.telNum"/>
	</view>
  <view class="cu-form-group">
		<view class="title">地址选择</view>
		<region-picker @change="regionChange" :value="region" :custom-item="customItem">
			
<view class="picker">
				{{region[0]}}，{{region[1]}}，{{region[2]}}
			</view>

		</region-picker>
	</view>
  <view class="cu-form-group">
		<view class="title">详细地址</view>
		<input placeholder="请输入详细地址" name="detailInfo" :value="userAddress.detailInfo"/>
	</view>
  <view class="cu-form-group">
		<view class="title">设为默认地址</view>
		<switch class="red sm" :checked="userAddress.isDefault == '1'" @change="isDefaultChange"></switch>
	</view>
  <button class="cu-btn block bg-green margin-sm" formType="submit">
    立即保存
  </button>
  <button class="cu-btn block bg-red margin-sm" @tap="userAddressDelete" v-if="userAddress.id">
    删除
  </button>
  <button class="cu-btn block bg-orange margin-sm cuIcon-weixin" @tap="getWxAddress">
    导入微信地址
  </button>
</form>
</block>
</template>

<script>
const app = getApp();
export default {data() {
            return {
              region: [],
              userAddress: [],
              customItem: ""
            };
        },onLoad(options) {
  // 本地获取参数信息
  let that = this;
  uni.getStorage({
    key: 'param-userAddressForm',
    success: function (res) {
      let userAddress = res.data;
      that.region[2]=userAddress.countyName ? userAddress.countyName : '选择区'
      that.region[1]=userAddress.cityName ? userAddress.cityName : '选择市'
      that.region[0]=userAddress.provinceName ? userAddress.provinceName : '选择省'
      that.setData({
        userAddress: userAddress
      });
    }
  });
}, 
methods: {
    regionChange(e) {
      this.setData({
        region: e.detail.value
      });
    },

    isDefaultChange(e) {
      if (e.detail.value) {
        this.userAddress.isDefault='1'
      } else {
        this.userAddress.isDefault='0'
      }
    },

    userAddressSave(e) {
      let value = e.detail.value;
      let region = this.region;

      if (!value.userName) {
        uni.showToast({
          title: '请填写收货人姓名',
          icon: 'none',
          duration: 3000
        });
        return;
      }

      if (!value.telNum) {
        uni.showToast({
          title: '请填写联系电话',
          icon: 'none',
          duration: 3000
        });
        return;
      }

      if (!/^1(3|4|5|7|8|9|6)\d{9}$/i.test(value.telNum)) {
        uni.showToast({
          title: '请输入正确的手机号码',
          icon: 'none',
          duration: 3000
        });
        return;
      }

      if (region[0] == '选择省') {
        uni.showToast({
          title: '请选择所在地区',
          icon: 'none',
          duration: 3000
        });
        return;
      }

      if (!value.detailInfo) {
        uni.showToast({
          title: '请填写详细地址',
          icon: 'none',
          duration: 3000
        });
        return;
      }

      app.globalData.api.userAddressSave({
        id: this.userAddress.id,
        userName: value.userName,
        telNum: value.telNum,
        provinceName: region[0],
        cityName: region[1],
        countyName: region[2],
        detailInfo: value.detailInfo,
        isDefault: this.userAddress.isDefault
      }).then(res => {
        uni.navigateBack();
      });
    },

    userAddressDelete() {
      let that = this;
      uni.showModal({
        content: '确认将这个地址删除吗？',
        cancelText: '我再想想',
        confirmColor: '#ff0000',

        success(res) {
          if (res.confirm) {
            app.globalData.api.userAddressDel(that.data.userAddress.id).then(res => {
              uni.navigateBack();
            });
          }
        }

      });
    },

    /*
    * 导入微信地址
    */
    getWxAddress: function () {
      let that = this;
      uni.authorize({
        scope: 'scope.address',
        success: function (res) {
          uni.chooseAddress({
            success: function (res) {
              that.userAddress.detailInfo=res.detailInfo
              that.userAddress.telNum=res.telNumber
              that.userAddress.userName=res.userName
              that.region[2]=res.countyName
              that.region[1]=res.cityName
              that.region[0]=res.provinceName
            },
            fail: function (res) {
              if (res.errMsg == 'chooseAddress:cancel') {
                uni.showToast({
                  title: '取消选择',
                  icon: 'none',
                  duration: 3000
                });
              }
            }
          });
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
}};
</script>
<style >

</style>